package com.smart.dao;

import com.smart.domain.goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jack on 2015/1/7.
 */
@Repository
public class GoodsDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    private  List<goods> goodsList = new ArrayList<goods>();

    public List<goods> GetGoodsList(int page){
        int currentpage = (page-1)*6;
        String sql = "SELECT * FROM goods LIMIT "+currentpage +","+6;
        goodsList =jdbcTemplate.query(sql, new ResultSetExtractor<List<goods>>() {
            @Override
            public List<goods> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
                List<goods> result = new ArrayList<goods>();
                while (resultSet.next())
                {
                    goods goods =new goods();
                    goods.setId(resultSet.getInt(1));
                    goods.setName(resultSet.getString(2));
                    goods.setPrice(resultSet.getDouble(3));
                    goods relateObject =new goods( resultSet.getString(5));
                    goods.setRelate(relateObject);
                    goods.setImg_url(resultSet.getString(6));
                    result.add(goods);
                }
                return result;
            }
        });


        return  goodsList;
    }

    public int getTotalPage(){
        String sql = "SELECT COUNT(*) FROM goods";
        int total = jdbcTemplate.queryForInt(sql);
        int page = total/6+1;
        return page;
    }

    public  List getAllType(){
        String sql = "SELECT DISTINCT type FROM goods";
        final List<String> list = new ArrayList<String>();
       jdbcTemplate.query(sql, new RowCallbackHandler() {
           @Override
           public void processRow(ResultSet resultSet) throws SQLException {
            String typeName = resultSet.getString("type");
               System.out.print(typeName);

               list.add(typeName);
           }
       });
        return list;
    }
    public  List getGoodsByType(String key){
        String sql = "SELECT  * FROM goods where type='"+key+"'";
        List<goods> list = new ArrayList<goods>();
        list =jdbcTemplate.query(sql,new ResultSetExtractor<List<goods>>() {
            @Override
            public List<goods> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
                List<goods> result = new ArrayList<goods>();
                while (resultSet.next())
                {
                    goods goods =new goods();
                    goods.setId(resultSet.getInt(1));
                    goods.setName(resultSet.getString(2));
                    goods.setPrice(resultSet.getDouble(3));
                    goods relateObject =new goods( resultSet.getString(5));
                    goods.setRelate(relateObject);
                    goods.setImg_url(resultSet.getString(6));
                    result.add(goods);
                }
                return result;
            }
        });


        return  list;
    }

}
